﻿'bram debouvere
'
Option Strict On

Imports Gift.BL
Imports System.Collections.Generic
Imports System.Data.OleDb

Public Class GiftListProduct
    Private _product As Product
    Private _giftlist As GiftList
    Private _category As String
    Private _price As Decimal
    Private _amount As Integer

    Public Sub New(ByVal Product As Product, ByVal GiftList As GiftList, ByVal Category As String, ByVal Price As Decimal, ByVal Amount As Integer)
        _product = Product
        _giftlist = GiftList
        _category = Category
        _price = Price
        _amount = Amount
    End Sub

    Public Property Product As Product
        Get
            Return _product
        End Get
        Set(ByVal Value As Product)
            _product = Value
        End Set
    End Property

    Public Property Giftlist As GiftList
        Get
            Return _giftlist
        End Get
        Set(ByVal Value As GiftList)
            _giftlist = Value
        End Set
    End Property

    Public Property Category As String
        Get
            Return _category
        End Get
        Set(ByVal Value As String)
            _category = Value
        End Set
    End Property

    Public Property Price As Decimal
        Get
            Return _price
        End Get
        Set(ByVal Value As Decimal)
            _price = Value
        End Set
    End Property

    Public Property Amount As Integer
        Get
            Return _amount
        End Get
        Set(ByVal Value As Integer)
            _amount = Value
        End Set
    End Property

    Public Overrides Function ToString() As String
        Return Product.Naam & vbTab & _category & vbTab & _amount & vbTab & Price
    End Function

    Public Shared Function getCategories(ByRef dbh As DatabaseHelper) As List(Of String)
        Dim list As New List(Of String)
        Dim reader As OleDbDataReader = dbh.GetReader("Select Distinct GLPCategorie as Categorie FROM GeschenklijstProduct", {})
        Do While reader.Read()
            list.Add(CStr(reader("Categorie")))
        Loop
        Return list
    End Function

    Shared Function isItemBesteld(ByRef GiftListProduct As GiftListProduct, ByRef dbh As DatabaseHelper) As Boolean
        Dim reader As OleDbDataReader = dbh.GetReader("SELECT * FROM BestellingGeschenklijstProduct WHERE BGLPProdId = ? AND BGLPLijstId = ?", {GiftListProduct.Product.Id, GiftListProduct.Giftlist.Id})
        Dim bool As Boolean = reader.Read()
        reader.Close()
        Return bool
    End Function

    Shared Function getAmountOrdered(ByRef GiftListProduct As GiftListProduct, ByRef dbh As DatabaseHelper) As Integer
        Dim reader As OleDbDataReader = dbh.GetReader("SELECT BGLPAantal FROM BestellingGeschenklijstProduct WHERE BGLPProdId = ? AND BGLPLijstId = ?", {GiftListProduct.Product.Id, GiftListProduct.Giftlist.Id})
        reader.Read()
        Dim int As Integer = CInt(reader("BGLPAantal"))
        reader.Close()
        Return int
    End Function

    Shared Function getItemsByGiftList(ByRef giftList As BL.GiftList, ByRef dbh As DatabaseHelper) As List(Of GiftListProduct)
        Dim list As New List(Of GiftListProduct)
        Dim reader As OleDbDataReader = dbh.GetReader("SELECT * FROM GeschenklijstProduct WHERE GLPLijstId=? ORDER BY GLPCategorie", {giftList.Id})
        Do While reader.Read()
            list.Add(New GiftListProduct(Product.getProductById(CInt(reader("GLPProdId")), dbh), giftList, CStr(reader("GLPCategorie")), CDec(reader("GLPPrijs")), CInt(reader("GLPAantal"))))
        Loop
        reader.Close()
        Return list
    End Function


End Class
